package architect.algorithn;

/**
 * 最长公共序列
 *
 * @author guanxin
 * @date 2018/5/8 10:22
 */

public class LongestCommonSequence {

    static int[][] martix;
    static String[][] flag;

    static String str1 = "afefafafwfafwa";
    static String str2 = "abcfefafewaff";

    public static void main(String[] args) {
        System.out.printf("当前最大公共子序列的长度为：%d \n", lcsl());
        // 坐标
        martix = new int[str1.length() + 1][str2.length() + 1];
        flag = new String[str1.length() + 1][str2.length() + 1];

        lcs2(str1, str2);
        // 打印子序列
        lcsc(str1.length(), str2.length());
    }

    static int lcsl() {
        martix = new int[str1.length() + 1][str2.length() + 1];

        lcs1(str1, str2);

        //只要拿出矩阵最后一个位置的数字即可
        return martix[str1.length()][str2.length()];
    }

    static void lcsc(int i, int j) {
        if (i == 0 || j == 0)
            return;

        if (flag[i][j] == "left_up") {
            System.out.printf("%s: 当前坐标:（%d, %d）\n", str2.charAt(j - 1), i - 1, j - 1);

            //左前方
            lcsc(i - 1, j - 1);
        } else {
            if (flag[i][j] == "up") {
                lcsc(i, j - 1);
            } else {
                lcsc(i - 1, j);
            }
        }
    }

    static void lcs2(String str1, String str2) {
        //初始化边界，过滤掉0的情况
        for (int i = 0; i <= str1.length(); i++)
            martix[i][0] = 0;

        for (int j = 0; j <= str2.length(); j++)
            martix[0][j] = 0;

        //填充矩阵
        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                //相等的情况
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    martix[i][j] = martix[i - 1][j - 1] + 1;
                    flag[i][j] = "left_up";
                } else {
                    //比较“左边”和“上边“，根据其max来填充
                    if (martix[i - 1][j] >= martix[i][j - 1]) {
                        martix[i][j] = martix[i - 1][j];
                        flag[i][j] = "left";
                    } else {
                        martix[i][j] = martix[i][j - 1];
                        flag[i][j] = "up";
                    }
                }
            }
        }
    }

    static void lcs1(String str1, String str2) {
        //初始化边界，过滤掉0的情况
        for (int i = 0; i <= str1.length(); i++)
            martix[i][0] = 0;

        for (int j = 0; j <= str2.length(); j++)
            martix[0][j] = 0;

        //填充矩阵
        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                //相等的情况
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    martix[i][j] = martix[i - 1][j - 1] + 1;
                } else {
                    //比较“左边”和“上边“，根据其max来填充
                    if (martix[i - 1][j] >= martix[i][j - 1])
                        martix[i][j] = martix[i - 1][j];
                    else
                        martix[i][j] = martix[i][j - 1];
                }
            }
        }
    }
}
